package com.activemanager.digitalTrain;

import java.io.InputStream;

import com.activemanager.gearCenter.GearCenter;
import com.activemanager.workoutAssistant.WorkoutAssistant_W1;
import com.example.android.navigationdrawerexample.R;
import com.example.android.navigationdrawerexample.R.layout;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class WelcomeCalibrating_DT0 extends Activity {
	static boolean dialog_sign = true;
	MyHandler myHandler = new MyHandler();
	TimeThread timeThread = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcomecalibrating_dt0);
		initPic();
		
		TextView tv_more = (TextView) findViewById(R.id.textView5);
		tv_more.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(WelcomeCalibrating_DT0.this, Calibrating_DT1.class);
				startActivity(intent);
				finish();
			}
			
		});
		ImageView img_home = (ImageView) findViewById(R.id.imageView);
		img_home.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}});
		
		timeThread = new TimeThread();
		timeThread.start();
	}

	private void initPic() {
		// TODO Auto-generated method stub
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		
		
//		RelativeLayout img = (RelativeLayout) findViewById(R.id.relativeLayout1);
//		InputStream is = getResources().openRawResource(
//				R.drawable.background_normal);
//		Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
//		img.setBackgroundDrawable(new BitmapDrawable(bit));

		ImageView img1 = (ImageView) findViewById(R.id.imageView1);
		InputStream is1 = getResources().openRawResource(
				R.drawable.d2_normal);
		Bitmap bit1 = BitmapFactory.decodeStream(is1, null, opt);
		img1.setImageBitmap(bit1);
		
		ImageView img2 = (ImageView) findViewById(R.id.imageView2);
		InputStream is2 = getResources().openRawResource(
				R.drawable.d4_normal);
		Bitmap bit2 = BitmapFactory.decodeStream(is2, null, opt);
		img2.setImageBitmap(bit2);
		
		RelativeLayout img3 = (RelativeLayout) findViewById(R.id.icon_rec);
//		InputStream is3 = getResources().openRawResource(
//				R.drawable.background_normal);
//		Bitmap bit3 = BitmapFactory.decodeStream(is3, null, opt);
//		img3.setBackgroundDrawable(new BitmapDrawable(bit3));
		img3.setBackgroundResource(R.drawable.bg_540);
	}

	private class MyHandler extends Handler {
		public void handleMessage(Message msg) {
			// �>�Message4�����β���Message������Էźܶ�����
			final BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true;
			
			final Dialog dialog = new Dialog(WelcomeCalibrating_DT0.this,
					R.style.MyDialog);
			dialog.setContentView(R.layout.p13);
			
			final RelativeLayout bg_img = (RelativeLayout) dialog
					.findViewById(R.id.linearLayout);
//			InputStream bg_is = getResources().openRawResource(R.drawable.u165_normal);
//			Bitmap bg_bit = BitmapFactory.decodeStream(bg_is, null, opt);
//			bg_img.setBackgroundDrawable(new BitmapDrawable(bg_bit));
			bg_img.setBackgroundResource(R.drawable.bg_for_popup_54);
			
			ImageView img = (ImageView) dialog
					.findViewById(R.id.imageView1);
			Button btn1 = (Button) dialog.findViewById(R.id.button1);
			Button btn2 = (Button) dialog.findViewById(R.id.button2);
			TextView tv = (TextView) dialog.findViewById(R.id.textView1);
			InputStream is = getResources().openRawResource(
					R.drawable.d_warning);
			Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
			img.setImageBitmap(bit);
			Window dialogWindow = dialog.getWindow();
			WindowManager m = getWindowManager();
			Display d = m.getDefaultDisplay();
			WindowManager.LayoutParams p = dialogWindow.getAttributes();
			p.height = (int) (d.getHeight() * 0.47);
			p.width = (int) (d.getWidth() * 0.95);
			dialogWindow.setAttributes(p);
			
			tv.setText("To get best effect of this feature,\n we strongly recommend connect\n to TV. Would you like to connect\n                          now?");
			btn1.setText("Connect Now");
			btn2.setText("Later");
			if(dialog_sign){
				dialog.show();
				dialog_sign = false;
			}
			
			


			btn2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					dialog.dismiss();
//					final Dialog dialog1 = new Dialog(WelcomeCalibrating_DT0.this,
//							R.style.MyDialog);
//					
//					dialog1.setContentView(R.layout.p20);
//					Button btn3 = (Button) dialog1.findViewById(R.id.button1);
//					Button btn4 = (Button) dialog1.findViewById(R.id.button2);
//					TextView tv = (TextView) dialog1.findViewById(R.id.textView1);
//
//					tv.setText(" To get best effect of this feature,\n we strongly recommend connect\n to TV. Would you like to connect\n                      now?");
//					btn3.setText("Connect Now");
//					btn4.setText("   Later   ");
//
//					btn4.setOnClickListener(new OnClickListener() {
//
//						@Override
//						public void onClick(View arg0) {
//							dialog1.dismiss();
//							Intent intent = new Intent();
//							intent.setClass(WelcomeCalibrating_DT0.this, Calibrating_DT1.class);
//							startActivity(intent);
//							finish();
//						}
//					});
//
//					btn3.setOnClickListener(new OnClickListener() {
//
//						@Override
//						public void onClick(View arg0) {
//							// TODO Auto-generated method stub
//							Intent intent = new Intent();
//							intent.setClass(WelcomeCalibrating_DT0.this, GearCenter.class);
//							startActivity(intent);
//							finish();
//						}
//
//					});
//					
//					Window dialogWindow = dialog.getWindow();
//					WindowManager m = getWindowManager();
//					Display d = m.getDefaultDisplay();
//					WindowManager.LayoutParams p = dialogWindow.getAttributes();
//					p.height = (int) (d.getHeight() * 0.5);
//					p.width = (int) (d.getWidth() * 0.9);
//					dialogWindow.setAttributes(p);
//					dialog1.show();
					
				}
			});

			btn1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent();
					intent.setClass(WelcomeCalibrating_DT0.this, GearCenter.class);
					startActivity(intent);
					finish();

				}

			});
		}
	}

	// �߳�--JAVA��ͬ�ģ���run��дҪ��������飡
	class TimeThread extends Thread {

		@Override
		public void run() {
			try {
				sleep(800);
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			Message sendMsg = myHandler.obtainMessage();
			sendMsg.what = 1;
			myHandler.sendMessage(sendMsg);
		}
	}
}
